﻿using System;
using System.Reflection;

namespace HCAD.CircularWorkingScaffoldAssistant.Component.Infrastructure
{
    public class Map<TInput, TOutput> : IMap<TInput, TOutput>
    {
        private readonly Func<TInput, TOutput> filter;

        public Map() {
            var propertyInfos = typeof(TInput).GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (var propertyInfo in propertyInfos) {
                if (propertyInfo.PropertyType == typeof(TOutput)) {
                    var methodInfo = propertyInfo.GetGetMethod();
                    filter = x => (TOutput)methodInfo.Invoke(x, new object[]{});
                    break;
                }
            }
        }

        public Map(Func<TInput, TOutput> filter) {
            this.filter = filter;
        }

        public void Input(TInput message) {
            Output(filter(message));
        }

        public event Action<TOutput> Output;
    }

    public class Map<TInput> : IMap<TInput>
    {
        public void Input(TInput message) {
            Output();
        }

        public event Action Output;
    }
}